package com.example.commandpattern.invoker;

import android.graphics.Canvas;

import com.example.commandpattern.draw.DrawPath;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 绘制请求封装类
 */
public class DrawInvoker {

    private List<DrawPath> drawList = Collections.synchronizedList(new ArrayList<DrawPath>());

    private List<DrawPath> undoList = Collections.synchronizedList(new ArrayList<DrawPath>());

    public void add(DrawPath path) {
        undoList.clear();
        drawList.add(path);
    }

    /**
     * 撤销
     */
    public void undo() {
        if (drawList.size() > 0) {
            DrawPath path = drawList.get(drawList.size() - 1);
            drawList.remove(drawList.size() - 1);
            path.undo();
            undoList.add(path);
        }
    }

    /**
     * 恢复上次的撤销
     */
    public void redo() {
        if (undoList.size() > 0) {
            DrawPath path = undoList.get(undoList.size() - 1);
            undoList.remove(undoList.size() - 1);
            drawList.add(path);
        }
    }

    public void execute(Canvas canvas) {
        if (drawList != null) {
            for (DrawPath path : drawList) {
                path.draw(canvas);
            }
        }
    }

    public boolean canRedo() {
        return undoList.size() > 0;
    }

    public boolean canUndo(){
        return drawList.size()>0;
    }

}
